<?php
require_once("header.php");
require_once("model/DB.php");
require_once("model/Kid.php");
require_once("model/User.php");
require_once("model/Grade.php");
require_once("model/Kid.php");
require_once("function/admin.login.php");

$parents = User::getUser("type","parent");
$grades = Grade::getGrade();
$kids = Kid::getKid();
?>
<script>
$(document).ready(function(){
	$("#add").click(function(){
		
		var div = $("#kid_form").clone(true);
		div.css("display","block");
		div.appendTo($("#add_form"));
	});
	
	$(".cancel").click(function(){
		
		$(this).parents().each(function(i){
			if(i==4){
				$(this).remove();
			}
		});
	});	
	
	$(".grade").change(function(){
		var td = $(this).parent().next().find("select");
		td.html("");
		var grade = $(this).find("select").val();
		if(!grade){
			td.attr("disabled",true);
			return false;
		}
		
		$.post(
			"function/getClass.php",
			{grade:grade},
			function(data){
				if(data){
					td.attr("disabled",false);
					td.append($("<option></option>").attr("value","auto").text("Auto"));
					for(var i=0;i<data.length;i++){
						td.append($("<option></option>").attr("value",data[i].id).text(data[i].title));
					}
				}
				else{
					td.attr("disabled",true);
				}
				
			},
			"json"
		);
	});
	
	$(".add_kid").submit(function(){
		var name = $(this).find(".name").val();
		var age = $(this).find(".age").val();
		var parent = $(this).find(".parent").val();
		var Class = $(this).find(".class").val();
		var weight = $(this).find(".weight").val();
		var height = $(this).find(".height").val();
		
		if(!name || !age || !parent || !Class){
			alert("Please complete the forms");
			return false;
		}
		
		$.post(
		 "function/addkid.php",
		 {name:name,age:age,parent:parent,Class:Class,weight:weight,height:height},
		 function(data){
			if(data){
				if(data=="added"){
					alert(name+" was added into database!");
					$("#kid_list").load("pieces/kidlist.php");
				}
				else{
					alert("Error!");
				}
			}
			else{
				alert("Connection Error!");
			}
		 }
		);
		return false;
		
	});
});
</script>
<a href="javascript:void(0)" id="add">Add Kids</a>
<div id="kid_list">
<table>
	<thead>
		<tr><td>Name</td><td>Age</td><td>Grade</td><td>Class</td><td>Parent</td><td>Action</td></tr>
	</thead>
	<tbody>
		<?php
		if($kids){

			foreach($kids as $kid){
				?>
				<tr><td><?php echo $kid['name']; ?></td><td><?php echo $kid['age']; ?></td><td><?php echo $kid['grade'] ?></td><td><?php echo $kid['class'] ?></td><td><?php echo $kid['username'] ?></td><td>edit delete</td></tr>
				<?php 
			}
		}
		?>
	</tbody>
</table>
</div>
<div id="kid_form" style="display:none">
<form class="add_kid">
<table>
	<tr>
		<td>*Student Name</td><td><input type="text" class="name"></td>
	</tr>
	<tr>
		<td>*Age</td><td><input type="text" class="age"></td>
	</tr>
	<tr>
		<td>*Parent</td><td><select class="parent"><option value="">Select Parent</option><?php if($parents): foreach($parents as $parent): ?><option value="<?php echo $parent['id']; ?>"> <?php echo $parent['username']; ?>  </option><?php endforeach; endif; ?></select></td>
	</tr>
	<tr>
		<td>*Grade</td><td  class="grade"><select ><option value="">Select a Grade</option><?php if($grades): foreach($grades as $grade): ?><option value="<?php echo $grade['id']; ?>"> <?php echo $grade['title']; ?>  </option><?php endforeach; endif; ?></select></td>
	</tr>	
	<tr>
		<td>*Class</td><td><select class="class" disabled></select></td>
	</tr>
	<tr>
		<td>Weight(kg)</td><td><input type="text" class="weight"></td>
	</tr>	
	<tr>
		<td>Height(cm)</td><td><input type="text" class="height"></td>
	</tr>	
	<tr>
		<td><input type="submit" value="Save"></td><td><input type="button" class="cancel" value="Cancel"></td>
	</tr>
</table>
</form>
</div>
<div id="add_form">
</div>